package es.ats.codington.festival.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;


public class EventUnSignUpServlet extends HttpServlet  {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession sesion = request.getSession(false);
		
		if (sesion == null){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/");
	        requestDispatcher.forward(request, response);
		}
	
		else{	
		
			EventSignUp esu = new EventSignUp();
			EventSignUpService ess = new EventSignUpService();
			Event ev=new Event();
			EventService es=new EventService();
		
			try {
	
				
					int  eventId =Integer.parseInt(request.getParameter("eventid"));
					int  visitorId = (Integer) sesion.getAttribute("peopleid");
			
					esu.setIdEvent(eventId);
					esu.setIdVisitor(visitorId);
					ev=es.getEventById(eventId);
			    
				
					boolean r=ess.isRegisteredToEvent(esu);
					//If the user is currently registered into the event 
					if(r == true){
					
						JOptionPane.showMessageDialog(null,"You are going to unregistered from this event. Are you sure?");
				        ev.setEventId(eventId);
				        int unregis=ess.unregisterVisitorForEventDAO(esu);
				        
				        //if it was impossible to unregister this visitor from the event
				        	if(unregis==0){
				        		JOptionPane.showMessageDialog(null,"Sorry, you couldn't be unregistered from "+ev.getName());
				        		request.setAttribute("event",ev);
								request.setAttribute("registered",true);
							
				        	}
				        //Visitor is now unregistered from that event
				        	else{
				        		
				        		JOptionPane.showMessageDialog(null,"You are now unregistered from "+ev.getName());
				        		request.setAttribute("event",ev);
								request.setAttribute("registered",false);
								
								
								
				        	}
				        
				        RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/showevent.jsp");
					    requestDispatcher.forward(request, response);
					
					}		
					//if the user is not register into the event yet
					else{
		
						JOptionPane.showMessageDialog(null,"You are not registered on this event yet!");
						List<Event> events = new ArrayList<Event>();
						events=ess.viewEventsOfUserDAO(esu.getIdVisitor());
						request.setAttribute("allevents",events);
						request.setAttribute("eventsize",events.size());
						request.setAttribute("tittle","Upcoming events that you are signed in");
						RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/events.jsp");
				        requestDispatcher.forward(request, response);
				
				
					}
			}
			catch (Exception e) {
				request.setAttribute("error", "Something is wrong with your dates. Please, try it again in a few minits.");
				RequestDispatcher rd = request.getRequestDispatcher("jsp/error.jsp");
				rd.forward(request,response);
			}
		}
		
	}
		
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	
	
}
